// @ts-nocheck
import router from '@system.router';
import prompt from '@system.prompt';
import http from '@ohos.net.http';

export default {
    data: {
        goodsList: [
            {
                itemId: 0,
                num: 1,
                itemName: "",
                itemPrice: 0,
                itemDescription: "",
                itemImagePath: []
            }
        ],
        submitAvailable: true
    },
    computed: {
        goodsNum: function () {
            var num = 0
            for (let i = 0; i < this.goodsList.length; i++) {
                num += this.goodsList[i].num
            }
            return num
        },
        goodsCount: function () {
            var sum = 0
            for (let i = 0; i < this.goodsList.length; i++) {
                sum += this.goodsList[i].num * this.goodsList[i].itemPrice
            }
            return sum
        },
        orderItem: function () {
            var items = []
            for (let i = 0; i < this.goodsList.length; i++) {
                items.push({
                    itemId: this.goodsList[i].itemId,
                    itemNum: this.goodsList[i].num
                })
            }
            return items
        }
    },
    onInit() {
        this.goodsList = router.getParams().goods
    },
    submit() {
        if (this.submitAvailable) {
            this.submitAvailable = false
            this.submitOrder()
            this.submitAvailable = true
        }
    },
    submitOrder() {
        let httpRequest = http.createHttp();
        httpRequest.on('headerReceive', (err, data) => {
            if (!err) {
                console.info('header: ' + data.header);
            } else {
                console.info('error:' + err.data);
            }
        });

        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
        this.$app.$def.getOrderCreateUrl(),
            {
                method: 'POST', // 可选，默认为“GET”
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json',
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: {
                    orderUserId: this.$app.$def.globalData.user.userId,
                    orderItem: this.orderItem
                }
            }, (err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                var result = JSON.parse(data.result)
                if (result.code == 1) {
                    var that = this
                    getApp().globalData.cartData = getApp().globalData.cartData.filter(function (item) {
                        return that.goodsList.filter(function (goods) {
                            return goods.itemId == item.itemId
                        }).length == 0
                    })
                    prompt.showToast({
                        message: "订单创建成功!",
                        duration: 1500,
                    })

                    setTimeout(function () {
                        router.back({
                            params: {
                                message: "订单创建成功!"
                            }
                        })
                    }, 1500)

                } else {
                    prompt.showToast({
                        message: "订单提交失败，请检查网络之后重试"
                    })
                }
            } else {
                console.info('create order error:' + err.data);
                prompt.showToast({
                    message: "订单提交失败，请检查网络之后重试"
                })
            }
        });
    },
    back() {
        router.back()
    }
}
